package com.example.composestudy.ui.main

import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.filled.Star
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.composestudy.data.models.AnimatedData
import com.example.composestudy.data.models.DiscoveryData
import com.example.composestudy.data.models.HomeData
import com.example.composestudy.data.models.SettingsData
import com.example.composestudy.ui.animated.AnimateXPage
import com.example.composestudy.ui.animated.AnimatedContentPage
import com.example.composestudy.ui.animated.AnimatedTextPage
import com.example.composestudy.ui.animated.AnimatedVisibilityPage
import com.example.composestudy.ui.discovery.CanvasPage
import com.example.composestudy.ui.home.BasicKnowledgePageA
import com.example.composestudy.ui.home.BasicKnowledgePageB
import com.example.composestudy.ui.home.ListPage
import com.example.composestudy.ui.home.WellnessScreen
import com.example.composestudy.ui.navigation.Screens


enum class Destination(
    val route: String,
    val label: String,
    val icon: ImageVector,
    val contentDescription: String
) {
    Home(Screens.HomePage.route, "Home", Icons.Default.Home, "Home"),
    Discovery(Screens.DiscoveryPage.route, "Discovery", Icons.Default.Star, "Discovery"),
    Settings(Screens.SettingsPage.route, "Settings", Icons.Default.Settings, "Settings")
}

@Composable
fun MainTabsPage() {
    val navController = rememberNavController()
    Scaffold(bottomBar = {
        BottomNavigationBar(navController)
    }) { contentPadding ->
        AppNavHost(navController, Destination.Home, modifier = Modifier.padding(contentPadding))
    }
}

@Composable
fun BottomNavigationBar(navController: NavController) {
    var selectedDestination by rememberSaveable { mutableIntStateOf(Destination.Home.ordinal) }
    NavigationBar {
        Destination.entries.forEachIndexed { index, destination ->
            NavigationBarItem(
                selected = selectedDestination == index,
                onClick = {
                    navController.navigate(destination.route) {
                        popUpTo(navController.graph.startDestinationId) {
                            saveState = true
                        }
                    }
                    selectedDestination = index
                },
                icon = {
                    Icon(
                        imageVector = destination.icon,
                        contentDescription = destination.contentDescription
                    )
                },
                label = { Text(text = destination.label) }
            )
        }
    }
}

@Composable
fun AppNavHost(
    navController: NavHostController,
    startDestination: Destination,
    modifier: Modifier = Modifier
) {
    NavHost(
        navController,
        startDestination = startDestination.route
    ) {
        Destination.entries.forEach { destination ->
            composable(destination.route) {
                when (destination) {
                    Destination.Home -> ListPage(
                        data = HomeData.conversationHomeObject,
                        navTitle = "Jetpack Compose",
                        showBackIcon = false,
                        onBack = {}) { route ->
                        navController.navigate(route)
                    }

                    Destination.Discovery -> ListPage(
                        data = DiscoveryData.conversationHomeObject,
                        navTitle = "Jetpack Compose",
                        showBackIcon = false,
                        onBack = {}) { route ->
                        navController.navigate(route)
                    }

                    Destination.Settings -> ListPage(
                        data = SettingsData.conversationHomeObject,
                        navTitle = "Jetpack Compose",
                        showBackIcon = false,
                        onBack = {}) { route ->
                        navController.navigate(route)
                    }
                }
            }
        }

        composable(Screens.ListPage.route) {
            val pageKey = it.arguments?.getString("pageKey")
            if (pageKey != null) {
                if (pageKey == "AnimatedPageKey") {
                    ListPage(
                        data = AnimatedData.conversationHomeObject,
                        navTitle = "动画",
                        onBack = {
                            navController.popBackStack()
                        }) { route ->
                        navController.navigate(route)
                    }
                }
            } else {
                ListPage(
                    data = HomeData.conversationHomeObject,
                    navTitle = "Jetpack Compose",
                    showBackIcon = false,
                    onBack = {}) { route ->
                    navController.navigate(route)
                }
            }
        }

        composable(Screens.BasicKnowledgePageA.route) {
            BasicKnowledgePageA(onBack = { navController.popBackStack() })
        }

        composable(Screens.BasicKnowledgePageB.route) {
            BasicKnowledgePageB(onBack = { navController.popBackStack() })
        }

        composable(Screens.WellnessTasksPage.route) {
            WellnessScreen(onBack = { navController.popBackStack() })
        }

        /// animated
        composable(Screens.AnimateXPage.route) {
            AnimateXPage(onBack = { navController.popBackStack() })
        }

        composable(Screens.AnimatedVisibilityPage.route) {
            AnimatedVisibilityPage(onBack = { navController.popBackStack() })
        }

        composable(Screens.AnimateTextPage.route) {
            AnimatedTextPage(onBack = { navController.popBackStack() })
        }

        composable(Screens.AnimatedContentPage.route) {
            AnimatedContentPage(onBack = { navController.popBackStack() })
        }

        composable(Screens.CanvasPage.route) {
            CanvasPage { navController.popBackStack() }
        }
    }
}